package com.ruoyi.life.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.life.service.IZsCustomerService;
import com.ruoyi.omp.api.domain.ZsCustomer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 客户信息Controller
 *
 * @author cjie
 * @date 2021-08-03
 */
@Api(tags = " 客户信息")
@RestController
@RequestMapping("/zsCustomer")
public class ZsCustomerController extends BaseController {
    @Autowired
    private IZsCustomerService zsCustomerService;

    /**
     * 查询客户信息列表
     */
    @ApiOperation(value="查询客户信息列表", notes="查询客户信息列表")
    @PreAuthorize(hasPermi = "life:zsCustomer:list")
    @GetMapping("/list")
    public TableDataInfo list(ZsCustomer zsCustomer) {
        startPage();
        List<ZsCustomer> list = zsCustomerService.selectZsCustomerList(zsCustomer);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @ApiOperation(value="导出客户信息列表", notes="导出客户信息列表")
    @PreAuthorize(hasPermi = "life:zsCustomer:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZsCustomer zsCustomer) throws IOException {
        List<ZsCustomer> list = zsCustomerService.selectZsCustomerList(zsCustomer);
        ExcelUtil<ZsCustomer> util = new ExcelUtil<ZsCustomer>(ZsCustomer.class);
        util.exportExcel(response, list, "客户信息数据");
    }
    /**
     * 获取客户信息详细信息
     */
    @ApiOperation(value="获取客户信息详细信息", notes="获取客户信息详细信息")
    @PreAuthorize(hasPermi = "life:zsCustomer:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(zsCustomerService.selectZsCustomerById(id));
    }

    /**
     * 获取客户信息详细信息(内部调用)
     */
    @GetMapping("/getCustomer/{customerId}")
    public R<ZsCustomer> getCustomer(@PathVariable("customerId") String customerId) {
        return R.ok(zsCustomerService.selectZsCustomerById(customerId));
    }


    /**
     * 根据名字获取客户信息（内部调用）
     * @param customerName
     * @return
     */
    @GetMapping(value = "/getCustomerByName/{customerName}")
    R<ZsCustomer> getCustomerByName(@PathVariable("customerName") String customerName){
        ZsCustomer zsCustomer = new ZsCustomer();
        zsCustomer.setEnterpriseName(customerName);
        List<ZsCustomer> list =zsCustomerService.selectZsCustomerList(zsCustomer);
        if (list.size() > 0){
          return   R.ok(list.get(0));
        }else {
          return   R.ok(null);
        }
    }


    /**
     * 新增客户信息
     */
    @ApiOperation(value="新增客户信息", notes="新增客户信息")
    @PreAuthorize(hasPermi = "life:zsCustomer:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZsCustomer zsCustomer) {
        return toAjax(zsCustomerService.insertZsCustomer(zsCustomer));
    }

    /**
     * 新增客户信息(内部调用)
     */
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping("/addCustomer")
    public R addCustomer(@RequestBody ZsCustomer zsCustomer) {
        return toR(zsCustomerService.insertZsCustomer(zsCustomer));
    }

    /**
     * 修改客户信息
     */
    @ApiOperation(value="修改客户信息", notes="修改客户信息")
    @PreAuthorize(hasPermi = "life:zsCustomer:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZsCustomer zsCustomer) {
        return toAjax(zsCustomerService.updateZsCustomer(zsCustomer));
    }

    /**
     * 修改客户信息(内部调用)
     */
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateCustomer")
    public R updateCustomer(@RequestBody ZsCustomer zsCustomer) {
        return toR(zsCustomerService.updateZsCustomer(zsCustomer));
    }

    /**
     * 删除客户信息
     */
    @ApiOperation(value="删除客户信息", notes="删除客户信息")
    @ApiImplicitParam(name = "ids", value = "客户信息ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "life:zsCustomer:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(zsCustomerService.deleteZsCustomerByIds(ids));
    }
}
